package com.mmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.mmall.common.ServerResponse;
import com.mmall.dao.ShippingMapper;
import com.mmall.pojo.Shipping;
import com.mmall.service.IShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by hanchao on 2018/1/22.
 */
@Service("iShippingService")
public class ShippingImpl implements IShippingService {

    @Autowired
    private ShippingMapper shippingMapper;

    public ServerResponse add(Integer userId, Shipping shipping){
        shipping.setUserId(userId);
        int resultCount = shippingMapper.insert(shipping);
        if(resultCount>0 ){
            Map result = Maps.newHashMap();
            result.put("shippingId",shipping.getId());
            return ServerResponse.createdBySuccess(result);
        }
        return ServerResponse.createdByErrorMessage("添加失败");
    }
    public ServerResponse del(Integer userId, Integer shippingId){
       int resultCount = shippingMapper.deleteShippingIdUserId(userId, shippingId);
       if(resultCount > 0){
           return ServerResponse.createdBySuccessMessage("删除成功");
       }
        return ServerResponse.createdByErrorMessage("删除失败");
    }
    public ServerResponse update(Integer userId, Shipping shipping){
        shipping.setUserId(userId);
        int resultCount = shippingMapper.updateByShipping(shipping);
        if(resultCount>0 ){
            return ServerResponse.createdBySuccessMessage("更新成功");
        }
        return ServerResponse.createdByErrorMessage("更新失败");
    }

    public ServerResponse<Shipping> select(Integer userId, Integer shippingId){
        Shipping shipping = shippingMapper.selectByShippingIdUserId(userId, shippingId);
        if(shipping == null){
            return ServerResponse.createdByErrorMessage("无法查询到该地址");
        }
        return ServerResponse.createdBySuccess(shipping);
    }

    public ServerResponse<PageInfo> list(Integer userId ,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Shipping> shippingList = shippingMapper.selectByUserId(userId);
        PageInfo pageInfo = new PageInfo(shippingList);
        return ServerResponse.createdBySuccess(pageInfo);
    }
}
